<?php

namespace app\api\controller;

use app\BaseController;
use app\service\strangerApplyService;
use think\facade\Db;

class Admin extends BaseController
{
    public function test(){
        $res=send_sms("15055365697","杨战磊");
        var_dump($res);
    }
    public function strangerList(){
        $community_id=$this->request->staff["community_id"];
        $page=1;
        $keyword=request()->get("keyword");
        $where=[];
        $where['status'] =1;//状态
        if(!empty($keyword)){
            //$where
        }
        $userlist = (new strangerApplyService())->userAllData($community_id,$page,$where);
        parent::sendOutput(["data"=>$userlist,"staff"=>$this->request->staff]);
    }

    public function strangerInfo(){
        $id=request()->get("id");
        $userInfo = (new strangerApplyService())->getInfo($id);
        parent::sendOutput($userInfo);
    }

    //陌生人审核
    public function strangerAudit(){
        $apply_id=request()->post("apply_id");
        $type=request()->post("user_type");
        $applyInfo = (new strangerApplyService())->getInfo($apply_id);
        if(empty($applyInfo)){
            parent::sendOutput("用户信息错误",500);
        }

        $send=send_sms($applyInfo["mobile"],$applyInfo["name"]);
        $res=(new strangerApplyService())->audit($applyInfo,$this->request->staff,$type,$send['Message']);
        if($res){
            parent::sendOutput("操作成功");
        }else{
            parent::sendOutput("操作失败",500);
        }
    }
    public function staffOff(){
        $staff_id=$this->request->staff["id"];
        $staff=$this->request->staff;
        $res=Db::table("staff")->where("id",$staff_id)->update(["status"=>"off"]);
        if($res){
            $log["staff_id"]=$staff_id;
            $log["con"]=json_encode($this->request->staff);
            $log["name"]=$staff["name"];
            $log["mobile"]=$staff["mobile"];
            $log["operate_type"]="off";
            $log["create_time"]=date("Y-m-d H:i:s");
            Db::table("staff_log")->insert($log);
            parent::sendOutput("操作成功");
        }else{
            parent::sendOutput("操作失败",500);
        }
    }
    public function staffOn(){
        $staff_id=$this->request->staff["id"];
        $staff=$this->request->staff;
        $res=Db::table("staff")->where("id",$staff_id)->update(["status"=>"on"]);
        if($res){
            $log["staff_id"]=$staff_id;
            $log["con"]=json_encode($this->request->staff);
            $log["name"]=$staff["name"];
            $log["mobile"]=$staff["mobile"];
            $log["operate_type"]="on";
            $log["create_time"]=date("Y-m-d H:i:s");
            Db::table("staff_log")->insert($log);
            parent::sendOutput("操作成功");
        }else{
            parent::sendOutput("操作失败",500);
        }
    }
}